package com.fenxiangle.yueding.feature.focus.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.GenreClassBo;
import com.fenxiangle.yueding.entity.bo.GenreSeedBo;
import com.fenxiangle.yueding.entity.bo.OccupationBo;
import com.fenxiangle.yueding.feature.mine.view.adapter.PublishTypeAdapter;
import com.fenxiangle.yueding.common.widget.popupwindow.PublishGenrePopupWindow;
import com.fenxiangle.yueding.framework.DataServer;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Home on 2018/9/2.
 */

public class PublishGenreActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_publish)
    RecyclerView rvPublish;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout smartRefesh;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_publish_type;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {
        initToolBar(toolbar, "选择需求类型", true, true);


        AM.api().createApiService(UserApi.class)
                .getOccupation()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<OccupationBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(List<OccupationBo> occupationBos) {
                        dismissLoading();
                        if (occupationBos != null) {
                            PublishTypeAdapter publishTypeAdapter = new PublishTypeAdapter(occupationBos);
                            rvPublish.setAdapter(publishTypeAdapter);
                            publishTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    PublishGenrePopupWindow publishGenrePopupWindow = new PublishGenrePopupWindow(PublishGenreActivity.this);
                                    publishGenrePopupWindow.initPopupWindow(toolbar, occupationBos.get(position).getSecond());
                                    publishGenrePopupWindow.setCallback(new PublishGenrePopupWindow.Callback() {
                                        @Override
                                        public void getStr(String str) {
                                            if (!TextUtils.isEmpty(str)) {
                                                publishGenrePopupWindow.onDismiss();
                                                Intent intent = new Intent();
                                                intent.putExtra("three", publishTypeAdapter.getData().get(position).getOccupation() + "/" + str);
                                                setResult(2, intent);
                                                finish(); //结束当前的activity的生命周期

                                            }
                                        }
                                    });
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });

        rvPublish.setLayoutManager(new LinearLayoutManager(this));


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
